#include "DirectLOL/actions/basicactions.h"

BasicActions::BasicActions()
{
}

int BasicActions::MoveCamera(ViewPoint3D * pow, int* x, int* y)
{
    pow->angles->x(pow->angles->x()+(*x-250 )*0.05);
    pow->angles->y(pow->angles->y()+(*y-250 )*0.05);


    if(pow->angles->y()>90)
    {
      pow->angles->y(90);
    }
 /*   if(pow->teta<-90)
    {
        pow->angles->x(-90);
    }*/

    pow->SetVecteurDirecteur();
    SDL_WarpMouse(250, 250);
}

int BasicActions::MoveCameraN(ViewPoint3D * pow, int* x, int* y)
{
    pow->angles->x(pow->angles->x()-(*x-250)*0.05);
    pow->angles->y(pow->angles->y()-(*y-250) *0.05);

    if(pow->angles->y()>90)
    {
      pow->angles->y(90);
    }


    pow->SetVecteurDirecteur();
    SDL_WarpMouse(250, 250);
}


int BasicActions::MoveForwardA(Physic * physic, Vectorf * direction, float * coef)
{
    Vectorf * vectemp=new Vectorf(*direction);
    vectemp->y(0);
    vectemp->Normalize();
    *vectemp=*vectemp*(*coef);

    physic->AddAcceleration(vectemp);
}

int BasicActions::MoveDownWardA(Physic *physic, Vectorf *direction, float *coef)
{
    Vectorf * vectemp=new Vectorf(*direction);
    vectemp->y(0);
    vectemp->Normalize();
    *vectemp=*vectemp*(-*coef);

    physic->AddAcceleration(vectemp);
}

int BasicActions::StrafeLeftA(Physic *physic, Vectorf *direction, float *coef)
{
    Vectorf * vecb=new Vectorf(0.0,1.0,0.0);
    Vectorf * vec= new Vectorf(*direction);
    vec->y(0);
    Vectorf * directionlol= new Vectorf(0.0,0.0,0.0);
    *directionlol=(Vectorf::ProduitVectoriel(*vecb,*vec)).Normalize();

    *directionlol=*directionlol*(*coef);
    physic->AddAcceleration(directionlol);
}

int BasicActions::StrafeRightA(Physic *physic, Vectorf *direction, float *coef)
{
    Vectorf * vecb=new Vectorf(0.0,1.0,0.0);
    Vectorf * vec= new Vectorf(*direction);
    vec->y(0);
    Vectorf * directionlol= new Vectorf(0.0,0.0,0.0);
    *directionlol=(Vectorf::ProduitVectoriel(*vecb,*vec)).Normalize();

    *directionlol=*directionlol*(-*coef);
    physic->AddAcceleration(directionlol);

}

int BasicActions::MoveForward(Vectorf *position, Vectorf *direction, float coef)
{
    position->x(position->x()+direction->x()*coef);
    position->z(position->z()+direction->z()*coef);
}

int BasicActions::MoveDownWard(Vectorf *position, Vectorf *direction, float coef)
{
    position->x(position->x()-direction->x()*coef);
    position->z(position->z()-direction->z()*coef);
}

int BasicActions::StrafeLeft(Vectorf *position, Vectorf *direction, float coef)
{
    Vectorf * vecb=new Vectorf(0.0,1.0,0.0);
    Vectorf * vec= new Vectorf(*direction);
    vec->y(0);
    Vectorf * directionlol= new Vectorf(0.0,0.0,0.0);
    *directionlol=(Vectorf::ProduitVectoriel(*vecb,*vec)).Normalize();

    position->x(position->x()+directionlol->x()*coef);
    position->z(position->z()+directionlol->z()*coef);
}

int BasicActions::StrafeRight(Vectorf *position, Vectorf *direction, float coef)
{
    Vectorf * vecb=new Vectorf(0.0,1.0,0.0);
    Vectorf * vec= new Vectorf(*direction);
    vec->y(0);
    Vectorf * directionlol= new Vectorf(0.0,0.0,0.0);
    *directionlol=(Vectorf::ProduitVectoriel(*vecb,*vec)).Normalize();
    *directionlol=directionlol->Inverse();

    position->x(position->x()+directionlol->x()*coef);
    position->z(position->z()+directionlol->z()*coef);
}

// Effectue une rotation de 90 degré autours de l'axe z en x time

int BasicActions::Rotate(Vectorf *vec, int time)
{

}

int BasicActions::PushBoundingBox(AABB *a, AABB *b)
{
    /*! On doit définir 6 zones dans lesquelles la fonction pour "pousser" réagira différemment */


    if(a->IsUp(b))
    {
        /*
         *  Je récupère la différence entre aMin et la valeur de translation
         */

        float minn=(a->translationT1->y() + a->min->y());
        float maxx=(b->translationT1->y() + b->max->y());

        a->translationT1->y(a->translationT1->y()+ maxx - minn +0.001);
        a->physic->velocity->y(0);
    }

    else if (a->IsDown(b))
    {
        float minn=(b->translationT1->y() + b->min->y());
        float maxx=(a->translationT1->y()+ a->max->y());

        a->translationT1->y(a->translationT1->y()+ minn -maxx   -0.001);


    }
    else if(a->IsFront(b))
    {
        float minn=(b->translationT1->z() + b->min->z());
        float maxx=(a->translationT1->z()+a->max->z());

        a->translationT1->z(a->translationT1->z()+ minn - maxx    -0.01);

    }
    else if (a->IsBack(b))
    {
        float minn=(a->translationT1->z() + a->min->z());
        float maxx=(b->translationT1->z()+b->max->z());

        a->translationT1->z(a->translationT1->z()+ maxx - minn    +0.01);

        a->physic->velocity->z(0);

    }
    else if (a->IsRight(b))
    {
        float minn=(a->translationT1->x() + a->min->x());
        float maxx=(b->translationT1->x() + b->max->x());

        a->translationT1->x(a->translationT1->x()+ maxx - minn    +0.01);

        a->physic->velocity->x(0);
    }
    else if (a->IsLeft(b))
    {
        float minn=(a->translationT1->x() + a->max->x());
        float maxx=(b->translationT1->x() + b->min->x());

        a->translationT1->x(a->translationT1->x()+     maxx-minn  -0.01);

        a->physic->velocity->x(0);
    }
    else
    {
        *a->translationT1=*a->translation;
    }
}
